<?php
class Managebanner extends CI_Controller {
    function __construct() {
        parent::__construct();
        $id = $this->session->userdata('id');
        $adminname = $this->session->userdata('adminname');
        $hash = $this->session->userdata('hash');
        if (!empty($hash) && $hash == md5(SECRETCODE . $id . $adminname . SECRETCODE)) {
            $this->admin['name'] = $adminname;
            $this->admin['id'] = $id;
            $this->admin['hash'] = $hash;
            $this->load->model('news_m');
        } else {
            //die($id . ' ' . $adminname);
            redirect(base_url() . 'admin/login');
        }
        $this->load->model('banner_m');
    }
    
    function index(){
        $this->load->library('pagination');
        $offset = $this->uri->segment(3);
        $limit = 12;
        $config['base_url'] = site_url('admin/managebanner');
        $config['uri_segment'] = 3;
        $config['total_rows'] = $this->db->count_all('banner');
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $data['paginator'] = $this->pagination->create_links();
        $data['banner'] = $this->banner_m->get($limit,$offset,0);
        $this->load->view('admin/common/header');
        $this->load->view('admin/banner',$data);
        $this->load->view('admin/common/footer');
    }
    
    function add(){
        if(isset($_POST['add']) && $_POST['add'] == 'Add'){
            $data['order'] = $this->input->post('order');
            if($file = $this->_upload_files()){
                $data['path'] = base_url().'uploads/banner/'.$file;
                if($this->banner_m->add($data))
                    redirect(base_url().'admin/managebanner');
                else
                    die('Lỗi thêm ảnh mới!');
            }
        }else{
            $this->load->view('admin/common/header');
            $this->load->view('admin/banner_add');
            $this->load->view('admin/common/footer');
        }
  }
  
  function edit($id){
       if(isset($_POST['update']) && $_POST['update'] == 'Update'){
            $data['order'] = $this->input->post('order');
            $file = $this->_upload_files();
            
            if($file != null)
                $data['path'] = base_url().'uploads/banner/'.$file;
            if($this->banner_m->update($id,$data))
                redirect(base_url().'admin/managebanner');
            else
                die('Lỗi thêm ảnh mới!');
            
        }else{
            
            $data['banner'] = $this->banner_m->get(0,0,$id);
            
            $this->load->view('admin/common/header');
            $this->load->view('admin/banner_edit',$data);
            $this->load->view('admin/common/footer');
        }
  }
  
  function delete($id){
      if(!is_numeric($id) || $id <= 0)
          redirect(base_url().'admin/managebanner');
      if($this->banner_m->delete($id))
          redirect(base_url().'admin/managebanner');
  }
  
  public function _upload_files() {
        if ($this->admin['name'] != '' && $this->admin['id'] != '' && $this->admin['hash'] == md5(SECRETCODE . $this->admin['id'] . $this->admin['name'] . SECRETCODE)) {
            if (!file_exists('uploads/banner')) {
                mkdir('uploads/banner', 0777);
                chmod('uploads/banner', 0777);
            }
            
                
                $upload_cfg['max_size'] = '2048';
                $upload_cfg['upload_path'] = 'uploads/banner';
                $upload_cfg['allowed_types'] = 'gif|jpg|png';
                $upload_cfg['encrypt_name'] = FALSE;
                $upload_cfg['overwrite'] = FALSE;               
                $this->load->library('upload', $upload_cfg);
                
               if($this->upload->do_upload('banner_img')){ 
                        $img = $this->upload->data();
                          return $img['file_name'];
                }else
                    return null;
            
        } else
            redirect(base_url() . 'admin/login');
    }
}

?>
